package com.iweb;

import java.util.TreeSet;

public class Demo6 {
    public static void main(String[] args) {
        TreeSet<Animal> animals = new TreeSet<>((o1,o2)->{
            //o1 新节点
            //o2 原来的节点对象
            int num=o1.getAge()-o2.getAge();
            return num;
        });
        animals.add(new Animal("狮子",3));
        animals.add(new Animal("大象",31));
        animals.add(new Animal("狗子",10));
        animals.add(new Animal("猫",15));

        for (Animal a:animals){
            System.out.println(a);
        }
    }
}

class Animal{
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
